import { useState, useCallback, useEffect } from "react";

export interface VideoMaterial {
  track_name: string;
  start_time: number;
  duration: number;
  relative_index: number;
  material: {
    path: string;
    name: string;
    crop_settings: {
      upper_left_x: number;
      upper_left_y: number;
      upper_right_x: number;
      upper_right_y: number;
      lower_left_x: number;
      lower_left_y: number;
      lower_right_x: number;
      lower_right_y: number;
    };
  };
}

interface VideoMaterialManagerProps {
  onAutoSave?: (materials: VideoMaterial[]) => void;
  initialMaterials?: VideoMaterial[];
}

export const useVideoMaterialManager = ({
  onAutoSave,
  initialMaterials,
}: VideoMaterialManagerProps = {}) => {
  const [videoMaterials, setVideoMaterials] = useState<VideoMaterial[]>(
    initialMaterials || [
      {
        track_name: "主背景视频",
        start_time: 0,
        duration: 8000000,
        relative_index: 0,
        material: {
          path: "",
          name: "主背景视频",
          crop_settings: {
            upper_left_x: 0,
            upper_left_y: 0,
            upper_right_x: 1,
            upper_right_y: 0,
            lower_left_x: 0,
            lower_left_y: 1,
            lower_right_x: 1,
            lower_right_y: 1,
          },
        },
      },
    ],
  );

  // 更新视频素材
  const updateVideoMaterials = useCallback(
    (materials: VideoMaterial[]) => {
      setVideoMaterials(materials);
      if (onAutoSave) {
        onAutoSave(materials);
      }
    },
    [onAutoSave],
  );

  // 添加视频素材
  const addVideoMaterial = useCallback(() => {
    const newMaterial: VideoMaterial = {
      track_name: `视频${videoMaterials.length + 1}`,
      start_time: 0,
      duration: 8000000,
      relative_index: videoMaterials.length,
      material: {
        path: "",
        name: `视频${videoMaterials.length + 1}`,
        crop_settings: {
          upper_left_x: 0,
          upper_left_y: 0,
          upper_right_x: 1,
          upper_right_y: 0,
          lower_left_x: 0,
          lower_left_y: 1,
          lower_right_x: 1,
          lower_right_y: 1,
        },
      },
    };
    updateVideoMaterials([...videoMaterials, newMaterial]);
  }, [videoMaterials, updateVideoMaterials]);

  // 删除视频素材
  const removeVideoMaterial = useCallback(
    (index: number) => {
      const newMaterials = videoMaterials.filter((_, i) => i !== index);
      // 重新排序索引
      const reindexedMaterials = newMaterials.map((material, i) => ({
        ...material,
        relative_index: i,
      }));
      updateVideoMaterials(reindexedMaterials);
    },
    [videoMaterials, updateVideoMaterials],
  );

  // 更新单个视频素材
  const updateSingleVideoMaterial = useCallback(
    (index: number, updates: Partial<VideoMaterial>) => {
      const newMaterials = videoMaterials.map((material, i) =>
        i === index ? { ...material, ...updates } : material,
      );
      updateVideoMaterials(newMaterials);
    },
    [videoMaterials, updateVideoMaterials],
  );

  return {
    videoMaterials,
    setVideoMaterials: updateVideoMaterials,
    addVideoMaterial,
    removeVideoMaterial,
    updateSingleVideoMaterial,
  };
};
